package org.jeecg.modules.house;

import cn.hutool.core.util.StrUtil;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.selector.Html;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: TODO
 * @author: zhongyin
 * @date: 2021年02月28日 21:26
 */
public class PaHouse {

    private static class innerWebClient{
        private static final WebClient webClient = new WebClient();
    }

    /**
     * 获取指定网页实体
     * @param url
     * @return
     */
    public static HtmlPage getHtmlPage(String url){
        //调用此方法时加载WebClient
        WebClient webClient = innerWebClient.webClient;
        // 取消 JS 支持
        webClient.setJavaScriptEnabled(false);
        // 取消 CSS 支持
        webClient.setCssEnabled(false);
        HtmlPage page=null;
        try{
            // 获取指定网页实体
            page = (HtmlPage) webClient.getPage(url);
        }catch (MalformedURLException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return page;
    }


    public static String reurl(String id,String ipnum) {
        return "/item/" + recode(id,ipnum);
    }

    public static Integer SetObjNum(Integer n) {
        double a = 0;
        for (int i = 0; i < n; i++) {
            System.out.println(a);
            double rd = Math.floor(Math.random() * 10);
            System.out.println(rd);
            a += rd;

            System.out.println("=====");
        }
        return new Double(a).intValue();
    }

    public static String recode(String a,String ipnum) {
        String n = nscaler(a);
        Integer c = SetObjNum(a.length());
        Integer d = SetObjNum(a.length());
        Integer newn = Integer.parseInt(n) + d;
        String b = ipnum;
        b = nscaler(b);
        return c + "-" + newn + "-" + d + "-" + b;
    }

    public static String nscaler(String a) {
        String b = "";
        String[] arr = a.split("");
        for (int i = 0; i < arr.length; i++) {
            switch (arr[i]) {
                case "0":
                    b += "0";
                    break;
                case "1":
                    b += "2";
                    break;
                case "2":
                    b += "5";
                    break;
                case "3":
                    b += "8";
                    break;
                case "4":
                    b += "6";
                    break;
                case "5":
                    b += "1";
                    break;
                case "6":
                    b += "3";
                    break;
                case "7":
                    b += "4";
                    break;
                case "8":
                    b += "9";
                    break;
                case "9":
                    b += "7";
                    break;
            }
        }
        return b;
    }

    public static void main(String[] args) throws Exception {
        String a = "29359.16 平方米";
//        System.out.println(a.substring(0,-1));
//        System.out.println(a.substring(0,-2));
        System.out.println(a.substring(0,a.length()-4));

        String res = reurl("1396","1615204765312");
        System.out.println(res);
        // 获取指定网页实体
       /* HtmlPage page = getHtmlPage("https://newhouse.0557fdc.com/");

        List<HtmlElement> aList =(List<HtmlElement>)page.getByXPath("//li[@class='nav1']/a");
        for (HtmlElement aElement : aList) {
            System.out.println(aElement.asText());
            HtmlPage detailPage = aElement.click();
//            HtmlPage detailPage = (HtmlPage) currentPage.getEnclosingWindow().getTopWindow().getEnclosedPage();
//            String documentURI = detailPage.getDocumentURI();
//            System.out.println(documentURI);
            String s = detailPage.asXml();
            System.out.println(s);
            List<HtmlSpan> projectNames = (List<HtmlSpan>) detailPage.getByXPath("//span[@class='ProjectName']");
            String projectName = projectNames.get(0).asText();
            System.out.println(projectName);
            *//*List<HtmlElement> detailList = (List<HtmlElement>)detailPage.getByXPath("//div[@class='beihui']/a");
            for (HtmlElement htmlElement : detailList) {
                System.out.println(htmlElement.asXml());
            }*//*
        }*/
        HtmlPage detailPage = getHtmlPage("https://newhouse.0557fdc.com"+ reurl("1396","1615204765312"));
        List<HtmlSpan> projectNames = (List<HtmlSpan>) detailPage.getByXPath("//span[@class='ProjectName']");
        String projectName = projectNames.get(0).asText();
        System.out.println(projectName);
        List<HtmlUnorderedList> houseDetailList = (List<HtmlUnorderedList>) detailPage.getByXPath("//div[@class='Contentxt']/ul");
        for (HtmlUnorderedList htmlUnorderedList : houseDetailList) {
            System.out.println(htmlUnorderedList.asText());
        }
        /*List<HtmlDivision> detailList = (List<HtmlDivision>) detailPage.getByXPath("//div[@class='beihui']");
        for (HtmlDivision html : detailList) {
            System.out.println(html.asXml());
            List<HtmlElement> trList = html.getHtmlElementsByTagName("tr");
            for (HtmlElement element : trList) {
                List<HtmlElement> tdList = element.getHtmlElementsByTagName("td");
                for (HtmlElement tdElement : tdList) {
                    System.out.println(tdElement.asText());
                    String[] keyValue = tdElement.asText().split("：");
                    if(keyValue[0].equals("栋号")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    if(keyValue[0].equals("已备案")){
                        System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length()-1)));
                    }
                    if(keyValue[0].equals("许可证号码")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    if(keyValue[0].equals("已签约")){
                        System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length()-1)));
                    }
                    if(keyValue[0].equals("地上楼层")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    if(keyValue[0].equals("可售")){
                        System.out.println(StrUtil.trim((keyValue[1].substring(0,keyValue[1].length()-1)));
                    }
                    if(keyValue[0].equals("地下楼层")){
                        System.out.println(StrUtil.trim(keyValue[1]));
                    }
                    HtmlPage dongDetailPage = tdElement.click();
                    List<HtmlDivision> dongList = (List<HtmlDivision>) dongDetailPage.getByXPath("//div[@class='beihui']");
                }
            }
        }*/

        /*HtmlPage dongDetailPage =  getHtmlPage("https://newhouse.0557fdc.com/details/XegvPf4G5jgYKT7PfDMGINJKiWXefJ08wNc9ZDvROSNQUWwoXKNmK9eEYEAKSASTxjS4kn6zb75NnaedQOzL0pvnWEa4a9904rrbLNB3ZGmmQnUEIseY94B0frNDDVrh2snPjjr3xswX-NObHvufCRNC12JJ4s-MJVGSQrIkDh8=");
        List<HtmlUnorderedList> dongList = (List<HtmlUnorderedList>) dongDetailPage.getByXPath("//div[@class='content_xiangx']/ul");
        for (HtmlUnorderedList ulList : dongList) {
            System.out.println(ulList.asText());
            String[] keyValue = ulList.asText().split("：");
            if(keyValue[0].equals("套数")){
                System.out.println(keyValue[1].substring(0,keyValue[1].length() - 1));
            }
            if(keyValue[0].equals("规划许可证")){
                System.out.println(StrUtil.trim(keyValue[1]));
            }
            if(keyValue[0].equals("建筑结构")){
                System.out.println(StrUtil.trim(keyValue[1]));
            }
            if(keyValue[0].equals("预售许可面积")){
                System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length() - 4)));
            }
            if(keyValue[0].equals("网上销售总面积")){
                System.out.println(StrUtil.trim(keyValue[1].substring(0,keyValue[1].length() - 4)));
            }
            if(keyValue[0].equals("预售日期")){
                System.out.println(StrUtil.trim(keyValue[1]));
            }

        }*/

    }
}
